import type * as ts from 'typescript/lib/tsserverlibrary';
import { TemplateContext, TemplateLanguageService } from 'typescript-template-language-service-decorator';

export class TsTemplateTypescriptLanguageService implements TemplateLanguageService {
    constructor(
        private readonly languageService: ts.LanguageService,
        private readonly project: ts.server.Project
    ) {}

    // 获取补全
    getCompletionsAtPosition(
        context: TemplateContext,
        position: ts.LineAndCharacter
    ): ts.CompletionInfo {
        const logger = this.project.projectService.logger;
        // 获取完整的模板字符串内容
        const templateContent = context.text;
        // 获取当前位置
        const offset = context.toOffset(position);

        // 获取触发位置的节点
        const node = context.node;

        logger.info(`[DEBUG] 调用方法: qqqq getCompletionsAtPosition`);
        logger.info(`[DEBUG] 调用方法: templateContent ${templateContent}`);
        logger.info(`[DEBUG] 调用方法: offset ${offset}`);


        // 添加空的补全信息作为后备
        const emptyCompletions: ts.CompletionInfo = {
            isGlobalCompletion: false,
            isMemberCompletion: false,
            isNewIdentifierLocation: false,
            entries: []
        };

        const res = this.languageService.getCompletionsAtPosition(
            context.fileName,
            offset,
            undefined
        ) ?? emptyCompletions;
        // 使用 ?? 运算符提供后备值
        logger.info(`[DEBUG] 调用方法: qqqq fdfdd`);
        return res
    }
}
